<?php

/**
 * 打印输出数据|show的别名
 * @param $var
 */
function p($var)
{
    if (is_bool($var)) {
        var_dump($var);
    } else if (is_null($var)) {
        var_dump(NULL);
    } else {
        echo "<pre style='position:relative;z-index:1000;padding:10px;border-radius:5px;background:#F5F5F5;border:1px solid #aaa;font-size:14px;line-height:18px;opacity:0.9;'>" . print_r($var, true) . "</pre>";
        //        exit;
    }
}

/**
 * 获取单号信息
 * @param int $type
 * @return string
 */
function getKey($type = 1)
{
    $str = "";
    switch ($type){
        //行为id
        case 1:
            $str .= "TC";
            break;
        //锁粉规则id
        case 2:
            $str .= "SF";
            break;
        //场景规则id
        case 3:
            $str .= "SC";
            break;
    }

    $str .= date("YmdHis") . mt_rand(100000, 999999);

    return $str;
}

/**
 * 身份证号校验
 * @param $vStr
 * @return int
 */
function isCreditNo($vStr)
{
    $vCity = array(
        '11','12','13','14','15','21','22',
        '23','31','32','33','34','35','36',
        '37','41','42','43','44','45','46',
        '50','51','52','53','54','61','62',
        '63','64','65','71','81','82','91'
    );
    if (!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/', $vStr)) return 0;
    if (!in_array(substr($vStr, 0, 2), $vCity)) return 0;
    $vStr = preg_replace('/[xX]$/i', 'a', $vStr);
    $vLength = strlen($vStr);
    if ($vLength == 18) {
        $vBirthday = substr($vStr, 6, 4) . '-' . substr($vStr, 10, 2) . '-' . substr($vStr, 12, 2);
    } else {
        $vBirthday = '19' . substr($vStr, 6, 2) . '-' . substr($vStr, 8, 2) . '-' . substr($vStr, 10, 2);
    }
    if (date('Y-m-d', strtotime($vBirthday)) != $vBirthday) return 0;
    if ($vLength == 18) {
        $vSum = 0;
        for ($i = 17 ; $i >= 0 ; $i--) {
            $vSubStr = substr($vStr, 17 - $i, 1);
            $vSum += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr , 11));
        }
        if($vSum % 11 != 1) return 0;
    }
    return 1;
}


/**
 * 验证手机号是否正确
 * return bool
 */
function vali_phone($phone)
{
    $p = '/^1[0-9]{10}$/';
    if (!preg_match($p, $phone)) {
        return false;
    }
    return true;
}
